package com.wzq.permission.center.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wzq.framework.util.UtilValidate;
import com.wzq.permission.center.mapper.DataPermissionMapper;
import com.wzq.permission.center.mapper.OrganizeRoleMapper;
import com.wzq.permission.center.mapper.RoleMapper;
import com.wzq.permission.center.mapper.RoleMenuMapper;
import com.wzq.permission.center.mapper.UserRoleMapper;
import com.wzq.permission.client.model.Pager;
import com.wzq.permission.client.model.admin.DataPermissionModel;
import com.wzq.permission.client.model.admin.MenuModel;
import com.wzq.permission.client.model.admin.RoleDataPerissionModel;
import com.wzq.permission.client.model.admin.RoleMenuModel;
import com.wzq.permission.client.model.admin.RoleModel;
import com.wzq.permission.client.service.RoleService;
/**
 * 角色管理实现类
 * @author qzq
 *
 */
@Service
@Transactional
public class RoleServiceImpl  implements RoleService{

	@Autowired
	private RoleMapper mapper;
	@Autowired
	private RoleMenuMapper  roleMenuMapper;
	@Autowired
	private OrganizeRoleMapper organizeRoleMapper;
	@Autowired
	private UserRoleMapper userRoleMapper;
	@Autowired
	private DataPermissionMapper dataRoleMapper;
	
	@Override
	public void add(RoleModel role) {
		
		mapper.add(role);
	}

	@Override
	public void update(RoleModel role) {
		mapper.update(role);
		
	}

	@Override
	public void updateBySelective(RoleModel role) {
		
		mapper.updateBySelective(role);
		
	}

	@Override
	public void delete(Object id) {
		organizeRoleMapper.deleteOrgRoleByRoleId(Long.valueOf(id.toString()));
		mapper.delete(id);
		
	}

	@Override
	public int queryByCount(RoleModel role) {
	
		return mapper.queryByCount(role);
	}

	@Override
	public List<RoleModel> queryList(RoleModel role, Pager pager) {
		
		if(StringUtils.isNotEmpty(role.getRoleName())){
			role.setRoleName(role.getRoleName()+"%");
		}
		return  mapper.queryList(role, pager);
	}

	@Override
	public RoleModel queryById(Object id) {
	
		return  mapper.queryById(id);
	}

	@Override
	public void distributionSave(List<RoleMenuModel> roleMenuList, Long roleId) {
		
		if(UtilValidate.isNotEmpty(roleId)){//先删除原来的记录
			roleMenuMapper.deleteByRoleId(roleId);
		}
		if(UtilValidate.isNotEmpty(roleMenuList)){// 保存菜单 
			roleMenuMapper.saveRoleMenu(roleMenuList);
		}
		
	}

	@Override
	public List<String> getMenuIdByRoleId(Long roleId) {
		List<String> list=new ArrayList<String>();
		if(UtilValidate.isNotEmpty(roleId)){
			list=roleMenuMapper.getMenuIdByRoleId(roleId);
		}
		return list;
	}

	@Override
	public List<Long> getRoleDataPerissionByRoleId(Long roleId) {
		
		List<Long> list=new ArrayList<Long>();
		if(UtilValidate.isNotEmpty(roleId)){
			list=mapper.getRoleDataPerissionByRoleId(roleId);
		}
		return list;
	}

	@Override
	public List<DataPermissionModel> queryApplicationDataList(Long applicationId,Integer rangeType) {
		List<DataPermissionModel> dataList=new ArrayList<DataPermissionModel>();
		if(UtilValidate.isNotEmpty(applicationId)){
			 //dataList=mapper.queryApplicationDataList(applicationId ,rangeType);
			dataList=mapper.queryApplicationDataList(applicationId,rangeType );
		}
		return dataList;
	}

	@Override
	public void deleteDataPerissionByARoleId(Long roleId) {
		
		
	}

	@Override
	@Transactional
	public void saveDataPerssion(List<RoleDataPerissionModel> roleDataPerissionModelList,Long roleId) {
		mapper.deleteDataPerissionByroleId(roleId);
		mapper.saveDataPerission(roleDataPerissionModelList);
		
	}

	@Override
	public List<MenuModel> queryPermissionsByRoleId(Long roleId) {
		List<MenuModel> list=new ArrayList<MenuModel>();
		if(UtilValidate.isNotEmpty(roleId)){
			list=roleMenuMapper.queryPermissionsByRoleId(roleId);
		}
		return list;
	}

	@Override
	public void logicDelete(Long[] roleIds) {
		for(Long roleId:roleIds){
			//删除角色用户信息信息
			userRoleMapper.deleteUserByRoleId(roleId);
			//删除角色关联的组织信息
			organizeRoleMapper.deleteOrgRoleByRoleId(roleId);
			//删除菜单关联角色信息
			roleMenuMapper.deleteMenuByRoleId(roleId);
		}
		mapper.logicDelete(roleIds);
	}

    @Override
    public int isExist(RoleModel role) {
        return mapper.isExist(role);
    }
}
